

%prior to this need to manually segment the arteries using this line of
%code:
%vesselID = manualSegmentArteries(avios,trajdata,numart);
%you can run this for one of the imaging sessions and just copy vesselID to
%the other sessions since they'll be the same
%
% update from v1.0 - put the time2peak variable INTO the table (r); note
% this is calculated off of the max inten measurement and not the total
% inten
%

%start with datapointer which should have the mouseID, genotype, high
%restracking, and session numbers:
R = [];
vesselTotalIntensity = [];
surroundInten = [];
maxInten = [];
localgcamplevel = [];
gcamppixeladjustment = false;       %for some early iterations of the tracking, there was a pixel correction being applied to the gcamp which turned out not to be warranted; setting this to true will undo that correction but since 1/23/2024 no analysis should require this
dosave = true;                      %set to true to save the results in a table locally
prestimframes = 2:10;               %frames to use for the baseline (prior to stimulus)
stimframes = 11:39;                 %frames during which stimulus is applied
stimframes = 11:67; %this is for an 8 second stimulus
fps = 1/.14;                        %frames per second; for multispectral imaging this is from the time it takes to acquire ALL of the spectra

for n = 1:length(datapointer)
    for n2 = 1:length(datapointer(n).fullHighResTracking)
        [r,vesselTotalIntensity0,surroundInten0,maxInten0,validtraj,localgcamplevel0] = extractWidefieldTrajParams_v1_1(datapointer(n).fullHighResTracking{n2},...
            datapointer(n).mouseID,datapointer(n).sessionNums(n2),dosave,gcamppixeladjustment,prestimframes,stimframes,fps);
        switch datapointer(n).genotype
            case 0
                r.genotype = zeros(size(r,1),1);
            case 1
                r.genotype = ones(size(r,1),1);
        end
        vesselTotalIntensity0 = permute(vesselTotalIntensity0,[1,3,2]);
        vesselTotalIntensity = [vesselTotalIntensity;reshape(vesselTotalIntensity0,size(r,1),size(vesselTotalIntensity0,3))];
        
        surroundInten0 = permute(surroundInten0,[1,3,2]);
        surroundInten = [surroundInten;reshape(surroundInten0,size(r,1),size(surroundInten0,3))];
        
        maxInten0 = permute(maxInten0,[1,3,2]);
        maxInten = [maxInten;reshape(maxInten0,size(r,1),size(maxInten0,3))];

        localgcamplevel0 = permute(localgcamplevel0,[1,3,2]);
        localgcamplevel = [localgcamplevel;reshape(localgcamplevel0,size(r,1),size(localgcamplevel0,3))];
        R = [R;r];
    end
end


%% block to go through all the sessions and recalculate gcamp parameters based on the average gcamp during the stim ONLY
totalgcamp = [];
distance2gcamp = [];
SE = strel("disk",10);
for n = 1:length(datapointer)
    for n2 = 1:length(datapointer(n).fullHighResTracking)
        load(datapointer(n).fullHighResTracking{n2},'avgNormalizedGcampImages','traj','downsamplegcamp','micronsPerPixel','gcampDistanceScaling','avgParenchymaIOS_normalized')
        if ~exist('downsamplegcamp','var')
            downsamplegcamp = size(avgParenchymaIOS_normalized,1)/size(avgNormalizedGcampImages,1);
        end
        avgcamp = mean(avgNormalizedGcampImages,3);
        windowmask = avgcamp == 0;
        windowmask = imdilate(windowmask,SE);
        numtrials = length(traj);
        avgNormalizedGcampImages2 = avgNormalizedGcampImages.*repmat(~windowmask,1,1,numtrials);
        for n3 = 1:numtrials
            temp = normalizedNeuralActivityDistance_v1_2(avgNormalizedGcampImages2(:,:,n3),traj(1).vesselCent/downsamplegcamp,micronsPerPixel*downsamplegcamp,gcampDistanceScaling,'measured',false);
            totalgcamp = [totalgcamp;temp.distanceWeightedActivity'];
            distance2gcamp = [distance2gcamp;temp.distance2activity'];
        end
        clear downsamplegcamp avgNormalizedGcampImages
    end
    disp(n/length(datapointer))
end





